home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / lrdchelp.zip / LORDLOCK.C < prev    next >
C/C++ Source or Header  |  1995-04-08  |  3KB  |  114 lines

  1. /* LORDLOCK.C
  2. ** LORD's locking routines translated to C
  3. **
  4. ** Pascal version copyright is held by Seth Able Robinson
  5. **
  6. ** C version - Copyright 1995 by John Hutton.  All rights reserved.
  7. ** You have my permission to include this code in your own programs
  8. ** whether commercial or noncommercial.
  9. **
  10. ** Note that this module does not run on it's own.  It's meant to be
  11. ** part of a larger project.  Refer to Borland's documentation on
  12. ** project files
  13. */
  14.  
  15. #include <dir.h>
  16. #include <dos.h>
  17. #include <io.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include "lordlock.h"
  21.  
  22.  
  23. /* This function expects to be passed the name of the file that you want
  24. ** access to. For example... 
  25. **
  26. ** get_access_hard( "PLAYER.DAT" );
  27. **
  28. ** It checks for the presence of a locking semaphore file, PLAYER.DAX in
  29. ** this case, waits up to three seconds if it finds one, and overwrites
  30. ** it, or if it doesn't find one, creates it immediately.
  31. */
  32.  
  33. void get_access_hard( char *file_name )
  34.   {
  35.   char temp[128];
  36.   struct ffblk ffblk;
  37.   int done, counter = 0;
  38.   FILE *f;
  39.  
  40.   strcpy( temp, file_name );
  41.   temp[ strlen( temp ) - 1] = 'X';
  42.  
  43.   done = findfirst( temp, &ffblk,0);
  44.   while ((!done) && (counter < 3))
  45.     {
  46.     sleep( 1 );
  47.     done = findfirst( temp, &ffblk, 0);
  48.     counter++;
  49.     }
  50.  
  51.   f = fopen( temp, "w");          /* create the semaphore file */
  52.   fclose( f );
  53.  
  54.   }
  55.  
  56. /* This function deletes the locking semaphore file, signaling that
  57. ** other nodes may access the file now.  It expects the name of the
  58. ** data file to release.  For example...
  59. **
  60. ** unlock_file( "PLAYER.DAT" );
  61. */
  62.  
  63. void unlock_file( char *file_name )
  64.   {
  65.   char temp[128];
  66.  
  67.   strcpy( temp, file_name);
  68.   temp[ strlen( temp ) - 1] = 'X';
  69.  
  70.   unlink( temp );
  71.   }
  72.  
  73. /* This is just a quicky file copy function.  You need to use this, or
  74. ** something like it, anytime the operation that you're going to perform
  75. ** on a data file could reasonably be expected to take more than an 
  76. ** an instant.  Commonly, if a player is browsing a data file, it's not
  77. ** going to be quick and you need to copy the data file so they can look
  78. ** at their leisure.
  79. **
  80. ** This function expects the name of the data file and the player's
  81. ** record number.  For example...
  82. **
  83. ** copy_file( "PLAYER.DAT", 12 );
  84. **
  85. ** This copies the contents of PLAYER.DAT to TEMP12 (remember to delete
  86. ** the temporary file when you're done so you don't clutter up the LORD 
  87. ** directory).
  88. */
  89.  
  90. int copyfile( char *from_file, int player_num)
  91.   {
  92.   char temp[128];
  93.   FILE *f, *g;
  94.   int holder;
  95.   struct ffblk ffblk;
  96.  
  97.   sprintf( temp, "TEMP%d", player_num);
  98.  
  99.   f = fopen( from_file, "rb" );
  100.   g = fopen( temp, "wb" );
  101.  
  102.   do
  103.     {
  104.     holder = fgetc( f );
  105.     fputc( holder, g );
  106.     } while (holder != EOF);
  107.  
  108.   fclose( f );
  109.   fclose( g );
  110.  
  111.   return !findfirst( temp, &ffblk, 0 );  /* return status - did it work? */
  112.  
  113.   }
  114.